//
//  KABadgeViewController.m
//  KeepAccount
//
//  Created by Veer on 2017/12/26.
//  Copyright © 2017年 Veer. All rights reserved.
//

#import "KABadgeViewController.h"

#import "KAAddAccountCollectionCell.h"
#import "KABadgeCollectionHeaderView.h"
#import "KABadgeModel.h"
#import "KABadgeSectionModel.h"

@interface KABadgeViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, weak) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray<KABadgeSectionModel *> *badgeSectionModelArray;

@property (nonatomic, assign) NSInteger type;

@end

@implementation KABadgeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"徽章";
    
    self.badgeSectionModelArray = [NSMutableArray array];
    
    [self setupSubView];
    [self getData];
}

- (void)setupSubView
{
    
    CGFloat itemW = 86;
    CGFloat itemH = 110;
    NSInteger itemCount = 3;
    CGFloat margin = (SCREEN_WIDTH - itemW * itemCount) / (itemCount * 2);
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, 50);
    layout.footerReferenceSize = CGSizeMake(kScreenWidth, 10);
    layout.minimumInteritemSpacing = margin * 2;
    layout.minimumLineSpacing = 20;
    layout.sectionInset = UIEdgeInsetsMake(0, margin, 20, margin);
    //    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(itemW, itemH);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.alwaysBounceVertical = YES;
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [collectionView registerClass:[KAAddAccountCollectionCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [collectionView registerClass:[KABadgeCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    [collectionView registerClass:[KABadgeCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"reusableViewFooter"];
}


#pragma mark - data
- (void)getData
{
    NSMutableArray *badgeModelArray = [[KADataBase sharedDataBase] getAllBadgeModel];

    NSArray *typeNameArray = @[@"连续打卡", @"累计记账天数", @"累计记账笔数"];
    for (int i = 0; i < 3; i++) {
        
        KABadgeSectionModel *sectionModel = [[KABadgeSectionModel alloc] init];
        sectionModel.badgeModelArray = [NSMutableArray array];
        sectionModel.name = typeNameArray[i];
        sectionModel.type = i + 1;
        [self.badgeSectionModelArray addObject:sectionModel];
    }
    
    
    for (KABadgeModel *badgeModel in badgeModelArray) {
        
        KABadgeSectionModel *sectionModel = self.badgeSectionModelArray[badgeModel.type - 1];
        sectionModel.hadCount += badgeModel.status;
        [sectionModel.badgeModelArray addObject:badgeModel];
    }
    
    [self.collectionView reloadData];
}

#pragma mark - collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.badgeSectionModelArray.count;
}

//通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        KABadgeSectionModel *sectionModel = self.badgeSectionModelArray[indexPath.section];
        
        KABadgeCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
        headerView.sectionModel = sectionModel;
        
        return headerView;
    }else{
        
        KABadgeCollectionHeaderView *footerrView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"reusableViewFooter" forIndexPath:indexPath];
        if (indexPath.section == self.badgeSectionModelArray.count - 1) {
            footerrView.backgroundColor = [UIColor whiteColor];
        }else{
            footerrView.backgroundColor = RGB(245, 245, 245);
        }
        
        
        return footerrView;
    }
    
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    KABadgeSectionModel *sectionModel = self.badgeSectionModelArray[section];
    return sectionModel.badgeModelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    KABadgeSectionModel *sectionModel = self.badgeSectionModelArray[indexPath.section];
    KABadgeModel *badgeModel = sectionModel.badgeModelArray[indexPath.row];
    
    KAAddAccountCollectionCell *cell = (KAAddAccountCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    cell.badgeModel = badgeModel;
    
    return cell;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
}



@end
